home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / creator changer / creator changer project / source / progressbar.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  6.5 KB  |  199 lines

  1. import java.awt.*;
  2. import java.net.URL;
  3.  
  4. /**
  5.  * This is a simple Macintosh-like lightweight progress bar bean.
  6.  * This version does not support a disabled or background look,
  7.  * nor does it support different Appearance Manager settings.
  8.  * Please note, this is a simple example and was not written
  9.  * to use a different image, nor was it written with extensibility
  10.  * in mind.
  11.  * @author Levi Brown
  12.  * @author Apple Worldwide Developer Technical Support
  13.  * @author Apple Computer Inc.
  14.  * @version 1.0 10/29/1998
  15.  */
  16. public class ProgressBar extends BufferedDrawer
  17. {
  18.     /**
  19.      * Creates a new ProgressBar with zero percent.
  20.      */
  21.     public ProgressBar()
  22.     {
  23.         //Initialize our data members
  24.         percent                = 0;
  25.         progressImage        = Util.loadImage(progressImagePath, this);
  26.     }
  27.  
  28.     /**
  29.      * Sets the percentage complete this progress bar will indicate.
  30.      * @see #getPercent
  31.      */
  32.     public void setPercent(double percent)
  33.     {
  34.         if (percent < 0 || percent > 1)
  35.             throw new IllegalArgumentException("" + percent + " needs to be greater than zero and less than or equal to one.");
  36.         
  37.         this.percent = percent;
  38.         repaint();
  39.     }
  40.     
  41.     /**
  42.      * Gets the percentage complete this progress bar is currently indicating.
  43.      * @see #setPercent
  44.      */
  45.     public double getPercent()
  46.     {
  47.         return percent;
  48.     }
  49.  
  50.     /** 
  51.      * Returns the preferred size of this component.
  52.      * Overriden here to constrain height.
  53.      * @see java.awt.Component#getMinimumSize
  54.      * @see LayoutManager
  55.      */
  56.     public Dimension getPreferredSize()
  57.     {
  58.         Dimension s = getSize();
  59.         s.height = BAR_HEIGHT;
  60.         
  61.         return s;
  62.     }
  63.  
  64.     /**
  65.      * Renders the progress bar in an offscreen buffer.
  66.      * Renders the bar to indicate the current percent complete.
  67.      * @see #paint
  68.      * @see #setPercent
  69.      * @see #getPercent
  70.      */
  71.     protected void draw()
  72.     {
  73.         //Make sure we give the super a chance to do what it needs to.
  74.         super.draw();
  75.         
  76.         //Get the size of the ofscreen image.
  77.         int imageWidth    = workingImage.getWidth(this);
  78.         int imageHeight    = workingImage.getHeight(this);
  79.         //Determine the location of the percent to indicate
  80.         int progressAreaWidth = imageWidth - 4;
  81.         int progressLocation = (int)((percent * progressAreaWidth) + 2.5);
  82.                 
  83.         //Erase the contents.
  84.         workingGraphics.setColor(gray1);
  85.         workingGraphics.fillRect(0, 0, imageWidth, imageHeight);
  86.  
  87.         //Draw the progress indicator
  88.         
  89.         //If there is not enough room to show the head and tail, just draw a flat color.
  90.         if (progressLocation < 9)
  91.         {
  92.             //Draw the head (for the rightmost edge)
  93.             workingGraphics.drawImage(progressImage, progressLocation - 4, 2, (progressLocation - 4) + 6, 13,
  94.                                                      2, 0, 8, 10, this);
  95.             //Fill the area with a flat color, over the left part of the head (to obfuscate the three dimensional look). 
  96.             workingGraphics.setColor(colorE);
  97.             workingGraphics.fillRect(2, 2, progressLocation - 3, imageHeight - 4);
  98.         }
  99.         //There is enough room to draw the three dimensional look, so go ahead.
  100.         else
  101.         {
  102.             int fillStart    = 4;
  103.             int fillEnd        = progressLocation - 4;
  104.             
  105.             //Draw the fill
  106.             workingGraphics.setColor(colorA);
  107.             workingGraphics.drawLine(fillStart, 6, fillEnd, 6);
  108.             
  109.             workingGraphics.setColor(colorB);
  110.             workingGraphics.drawLine(fillStart, 5, fillEnd, 5);
  111.             workingGraphics.drawLine(fillStart, 7, fillEnd, 7);
  112.             
  113.             workingGraphics.setColor(colorC);
  114.             workingGraphics.drawLine(fillStart, 4, fillEnd, 4);
  115.             workingGraphics.drawLine(fillStart, 8, fillEnd, 8);
  116.             
  117.             workingGraphics.setColor(colorD);
  118.             workingGraphics.drawLine(fillStart, 3, fillEnd, 3);
  119.             workingGraphics.drawLine(fillStart, 9, fillEnd, 9);
  120.             
  121.             workingGraphics.setColor(colorE);
  122.             workingGraphics.drawLine(fillStart, 2, fillEnd, 2);
  123.             workingGraphics.drawLine(fillStart, 10, fillEnd, 10);
  124.             
  125.             workingGraphics.setColor(colorF);
  126.             workingGraphics.drawLine(fillStart, 11, fillEnd, 11);
  127.  
  128.             //Draw the tail
  129.             workingGraphics.drawImage(progressImage, 2, 2, 4, 13,
  130.                                                      0, 0, 2, 10, this);
  131.             
  132.             //Draw the head
  133.             workingGraphics.drawImage(progressImage, fillEnd, 2, fillEnd + 6 /*width of progress bar image*/, 13,
  134.                                                      2, 0, 8, 10, this);
  135.         }
  136.  
  137.         //Draw the border
  138.         workingGraphics.setColor(getBackground());
  139.         workingGraphics.drawLine(1, imageHeight - 1, imageWidth - 1, imageHeight - 1);
  140.         workingGraphics.drawLine(imageWidth - 1, 1, imageWidth - 1, imageHeight - 2);
  141.  
  142.         workingGraphics.setColor(gray0);
  143.         workingGraphics.drawLine(0, imageHeight - 1, 0, imageHeight - 1);
  144.         workingGraphics.drawLine(imageWidth - 1, 0, imageWidth - 1, 0);
  145.         
  146.         workingGraphics.setColor(gray2);
  147.         workingGraphics.drawLine(0, 0, 0, imageHeight - 2);
  148.         workingGraphics.drawLine(1, 0, imageWidth - 2, 0);
  149.         
  150.         workingGraphics.setColor(white);
  151.         workingGraphics.drawLine(imageWidth - 1, 1, imageWidth - 1, imageHeight - 1);
  152.         workingGraphics.drawLine(1, imageHeight - 1, imageWidth - 2, imageHeight - 1);
  153.  
  154.         workingGraphics.setColor(black);
  155.         workingGraphics.drawRect(1, 1, imageWidth - 3, imageHeight - 3);
  156.         
  157.     }
  158.  
  159.     /** 
  160.      * @deprecated As of JDK version 1.1,
  161.      * replaced by setBounds(int, int, int, int).
  162.      * Overriden here to constrain height.
  163.      */
  164.     public void reshape(int x, int y, int width, int height)
  165.     {
  166.         //Make sure to call the super's reshape method and not the
  167.         //super's setBounds method, or we end up in an infitite loop.
  168.         super.reshape(x, y, width, BAR_HEIGHT);
  169.     }
  170.  
  171.     //Hard coded colors to suppor the default Appearance Manager progress bar colors.
  172.     protected static final Color white = Color.white;
  173.     protected static final Color gray0 = new Color(222, 222, 222);
  174.     protected static final Color gray1 = new Color(189, 189, 189);
  175.     protected static final Color gray2 = new Color(173, 173, 173);
  176.     protected static final Color gray3 = new Color(140, 140, 140);
  177.     protected static final Color gray4 = new Color(82, 82, 82);
  178.     protected static final Color black = Color.black;
  179.  
  180.     protected static final Color colorA = new Color(239, 239, 239);
  181.     protected static final Color colorB = new Color(206, 206, 255);
  182.     protected static final Color colorC = new Color(156, 156, 255);
  183.     protected static final Color colorD = new Color(99, 99, 206);
  184.     protected static final Color colorE = new Color(49, 49, 156);
  185.     protected static final Color colorF = new Color(0, 0, 82);
  186.  
  187.     //Hard coded height for the progress bar.
  188.     protected static final int BAR_HEIGHT = 14;
  189.     //Relative location for the progress bar image file
  190.     protected static final String progressImagePath = "images/progressbar.gif";
  191.     
  192.     /**
  193.      * The current percentage the progress bar will display.
  194.      */
  195.     protected double percent;
  196.     //The loaded image representing the head and tail of the three dimensional progress bar.
  197.     protected Image progressImage;
  198. }
  199.